"use strict";
/*
 * @Author: wangcb
 * @Email: wangcbgg@163.com
 * @Date: 2020-09-01 08:46:44
 * @LastEditTime: 2021-10-21 17:16:04
 * 机顶盒播放器模拟器
 */
// 存放已生成实例id
const instanceIds = [];
// @ts-ignore 开发环境模拟MediaPlayer
if (globalThis?.process?.env.NODE_ENV === 'development') {
    class MediaPlayer {
        /**
         * 构造器
         * @param type 实例类型(针对stb播放器)
         */
        constructor(type) {
            /** 是否已释放 */ this.release = false;
            /** 播放地址 */ this.playUrl = '';
            /** 实例id */ this.instanceId = 0;
            /** 显示类型 */ this.videoDisplayMode = 1;
            /** left坐标 */ this.left = 0;
            /** top坐标 */ this.top = 0;
            /** width值 */ this.width = 1280;
            /** height值 */ this.height = 720;
            /**
             * 静音状态
             *
             * 0: 设置为有声 (默认值)
             *
             * 1: 设置为静音
             */
            this.muteFlag = 0;
            /** 音量值 */ this.volume = 50;
            if (instanceIds.length > 20)
                throw new Error('[MediaPlayer] instance exceeds the upper limit.');
            while (1) {
                const id = Math.ceil(Math.random() * 1000);
                if (instanceIds.indexOf(id) === -1) {
                    instanceIds.push(id);
                    this.instanceId = id;
                    break;
                }
            }
            this.video = document.createElement('video');
            this.video.innerHTML = `您的浏览器不支持<code>video</code>标签`;
            this.video.id = `mp_${this.instanceId}`;
            this.initMediaPlayer(this.instanceId, 0, 0, this.height, this.width, this.left, this.top, this.muteFlag, 0, 0, 0, 1, 0, 0);
        }
        initMediaPlayer(instanceId, playListFlag, videoDisplayMode, height, width, left, _top, muteFlag, useNativeUIFlag, subtitleFlag, videoAlpha, cycleFlag, randomFlag, autoDelFlag) {
            if (!this.release) {
                this.left = left, this.top = _top;
                this.video.width = width, this.video.height = height;
                this.video.style.cssText = `background:#000;position:absolute;z-index:-1;left:${this.left}px;top:${this.top}px`;
            }
        }
        setSingleMedia(playUrl) {
            if (!this.release) {
                this.playUrl = playUrl;
                this.video.src = this.playUrl;
                if (!document.body.contains(this.video)) {
                    document.body.appendChild(this.video);
                }
            }
        }
        /** 获取实例id */
        getNativePlayerInstanceID() {
            return this.instanceId;
        }
        /**
         * 释放播放器
         * @param instanceId 实例id
         */
        releaseMediaPlayer(instanceId) {
            this.stop();
            if (document.body.contains(this.video)) {
                document.body.removeChild(this.video);
            }
            this.release = true;
        }
        /** 从头起播 */
        playFromStart() {
            if (!this.release) {
                this.video.oncanplay = () => { this.resume(); };
            }
        }
        /**
         * 断点播放
         * @param type 播放类型
         * @param timestamp 断点
         * @param speed 速度
         */
        playByTime(type = 1, timestamp = 0, speed = 1) {
            if (!this.release) {
                let start = false;
                if (!start) {
                    const totalTime = this.getMediaDuration();
                    if (timestamp < 0)
                        timestamp = 0;
                    if (timestamp > totalTime)
                        timestamp = totalTime;
                    this.video.currentTime = timestamp;
                    this.resume();
                    start = true;
                }
            }
        }
        /** 暂停 */
        pause() {
            if (!this.release) {
                this.video.pause();
            }
        }
        /** 恢复播放 */
        resume() {
            if (!this.release) {
                this.video.play();
            }
        }
        /** 到末尾 */
        gotoEnd() {
            if (!this.release) {
                this.video.currentTime = this.getMediaDuration() - 5;
                this.resume();
            }
        }
        /** 到开头 */
        gotoStart() {
            if (!this.release) {
                this.video.currentTime = 0;
                this.resume();
            }
        }
        /** 停止播放 */
        stop() {
            if (!this.release) {
                this.video.pause();
                this.video.src = '';
            }
        }
        /**
         * 设置显示模式
         * @param videoDisplayMode 显示模式
         *
         * 0: 按 setVideoDisplayArea()中设定的Height, Width, Left, Top 属性所指定的位置和大小来显示视频
         *
         * 1: 全屏显示，按全屏高度和宽度显示(默认值)
         *
         * 2: 按宽度显示，指在不改变原有图像纵横比的情况下按全屏宽度显示
         *
         * 3: 按高度显示，指在不改变原有图像纵横比的情况下按全屏高度显示
         *
         * 255: 视频显示窗口将被关闭。它将在保持媒体流连接的前提下，隐藏视频窗口。如果流媒体播放没有被暂停，将继续播放音频
         */
        setVideoDisplayMode(videoDisplayMode = 1) {
            this.videoDisplayMode = videoDisplayMode;
        }
        /** 获取显示类型 */
        getVideoDisplayMode() {
            return this.videoDisplayMode;
        }
        /**
         * 设置显示位置及大小
         * @param _left left坐标
         * @param _top top坐标
         * @param _width width值
         * @param _height height值
         */
        setVideoDisplayArea(_left, _top, _width, _height) {
            this.left = _left;
            this.top = _top;
            this.width = _width;
            this.height = _height;
        }
        /** 获取left坐标值 */
        getVideoDisplayLeft() {
            return this.left;
        }
        /** 获取top坐标值 */
        getVideoDisplayTop() {
            return this.top;
        }
        /** 获取width值 */
        getVideoDisplayWidth() {
            return this.width;
        }
        /** 获取height值 */
        getVideoDisplayHeight() {
            return this.height;
        }
        /** 刷新显示使设置生效 */
        refreshVideoDisplay() {
            if (!this.release) {
                this.video.width = this.width;
                this.video.height = this.height;
                this.video.style.cssText = `background:#000;position:absolute;z-index:-1;left:${this.left}px;top:${this.top}px`;
            }
        }
        /**
         * 设置音量
         * @param volume 音量值
         */
        setVolume(volume) {
            if (!this.release) {
                this.volume = ~~volume;
                this.video.volume = this.volume / 100;
            }
        }
        /** 获取音量 */
        getVolume() {
            if (!this.release) {
                this.volume = (this.video.volume || 0) * 100;
                return this.volume;
            }
            return 0;
        }
        /**
         * 设置静音
         * @param muteFlag 静音标志
         */
        setMuteFlag(muteFlag) {
            if (!this.release) {
                this.muteFlag = muteFlag;
                this.video.muted = this.muteFlag === 1;
            }
        }
        /** 获取静音标志 */
        getMuteFlag() {
            if (!this.release) {
                const muted = this.video.muted;
                this.muteFlag = muted ? 1 : 0;
                return this.muteFlag;
            }
            return 0;
        }
        /** 获取当前播放的媒体的总时长 */
        getMediaDuration() {
            if (!this.release) {
                return Math.ceil(this.video.duration) || 0;
            }
            return 0;
        }
        /** 媒体播放到的当前时间点 */
        getCurrentPlayTime() {
            if (!this.release) {
                return Math.ceil(this.video.currentTime) || 0;
            }
            return 0;
        }
        /** 获取频道号(忽略) */
        getChannelNum() { return 0; }
        leaveChannel() { return 0; }
        setNativeUIFlag(flag) { }
        setMuteUIFlag(flag) { }
        setAudioVolumeUIFlag(flag) { }
        setAudioTrackUIFlag(flag) { }
        setProgressBarUIFlag(flag) { }
        setChannelNoUIFlag(flag) { }
        setAllowTrickmodeFlag(flag) { }
    }
    // @ts-ignore 开发环境自动挂载
    window.MediaPlayer = MediaPlayer;
}
